package com.srit.recipes.common.swagger2;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Operation;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Comparator;
import java.util.function.Predicate;


@Configuration
@EnableSwagger2
public class OpenApiAutoConfiguration {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        ApiInfo apiInfo =
                new ApiInfoBuilder()
                        .title("智能食谱")
                        .description("智能食谱api")
                        .version("1.0")
                        .contact(new Contact("叶叶叶", "", ""))
                        .license("apis")
                        .build();

        return new Docket(DocumentationType.OAS_30)
                .groupName("business-api")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.srit.recipes.controller"))
                .paths(new Predicate<String>() {
                    @Override
                    public boolean test(String s) {
                        return true;
                    }
                })
                .build()
                .operationOrdering(new Comparator<Operation>() {
                    @Override
                    public int compare(Operation o1, Operation o2) {
                        return 0;
                    }
                })
                .apiDescriptionOrdering(new Comparator<ApiDescription>() {
                    @Override
                    public int compare(ApiDescription o1, ApiDescription o2) {
                        return o1.getOperations().get(0).getSummary().compareTo(o2.getOperations().get(0).getSummary());
                    }
                })
                .apiInfo(apiInfo)
                ;
    }

}